package com.fdc_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdc_project.entity.Customer;
import com.fdc_project.mapper.CustomerMapper;
import com.fdc_project.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdc_project.vo.ErrorCode;
import com.fdc_project.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sk
 * @since 2021-11-29
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {


    @Autowired
    CustomerMapper customerMapper;


    @Override
    public Result add(Customer customer) {
        if(customerMapper.insert(customer)>0){
            return Result.success(customer);
        }
        return Result.fail(10001,"添加失败");
    }

    @Override
    public Result delete(Integer id) {
        Customer customer = customerMapper.selectById(id);
        if(customer == null || customer.getDeleted() == 1){
            return  Result.fail(ErrorCode.USER_NULL.getCode(),"客户不存在!");
        }
        customer.setDeleted(1);
        if(customerMapper.updateById(customer)>0){
            return Result.success("删除成功！");
        }
        else{
            return Result.fail(-300,"删除失败");
        }
    }

    @Override
    public Result update(Customer customer) {
        Customer customer1 = customerMapper.selectById(customer.getCusId());
        if(customer1 == null || customer1.getDeleted() == 1){
            return Result.fail(ErrorCode.USER_NULL.getCode(), "客户不存在!");
        }
        if(customerMapper.updateById(customer)>0){
            return Result.success("客户信息更新成功！");
        }
        else{
            return Result.fail(-300,"更新失败");
        }

    }

    @Override
    public Result selectone(Integer id) {
        Customer customer = customerMapper.selectById(id);
        if(customer == null ||customer.getDeleted() == 1){
            return  Result.fail(ErrorCode.USER_NULL.getCode(), "客户不存在！");
        }
            return Result.success(customer);
    }

    @Override
    public Result selectcall() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("deleted",0);
        List list = customerMapper.selectList(queryWrapper);
        if(list.size()>0){
            return Result.success(list);
        }
        else{
            return Result.fail(ErrorCode.USER_NULL.getCode(), "表中不存在客户！");
        }
    }
}
